package cn.meetingroom.service;

import cn.meetingroom.dao.ItemDao;
import cn.meetingroom.dao.RoomDao;
import cn.meetingroom.model.Item;
import cn.meetingroom.model.Room;

import java.util.List;

public class ItemService {
    private ItemDao itemDao = new ItemDao();
    private RoomDao roomDao = new RoomDao();
    /*public User getUser(long id){
        UserService userService = new UserService();
        return userService.getUserById(id);
    }*/

    public boolean addItem(Item item){
        if (itemDao.addItem(item)) return true;
        return false;
    }

    public boolean userDel(Long id){
        Item item = itemDao.find("id",id);
        if (item.getState() != 1){
            itemDao.delItem(id);
            return true;
        } else if (item.getState() == 1){
            itemDao.updateItem("state","0","id",id);
            return true;
        }
        return false;
    }

    public boolean statechange(long id,int state){
        return itemDao.changeState(id,state);
    }

    public int findRid(String colName,String value){
        return roomDao.find(colName,value).getId();
    }

    public Room getRoom(int id){
        return roomDao.find("id",id);
    }

    public boolean isExist(String roomNum,String data,int interval){
        RoomDao roomDao = new RoomDao();
        Room room = roomDao.find("room_num",roomNum);
        List<Item> items = itemDao.search("data",data);
        System.out.println(items);

        if (items != null && items.size() != 0){
            for (int i = 0; i < items.size(); i++) {
                if (items.get(i).getRoomId() == room.getId() && items.get(i).gettInterval() == interval)
                    return true;
            }
        }
        return false;
    }
}
